/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using System.Collections.Generic;
using EvaluationException = System.Qizx.Api.EvaluationException;
using Item = System.Qizx.Api.Item;
using Node = System.Qizx.Api.Node;
using XQValue = System.Qizx.XQuery.XQValue;
namespace System.Qizx.Xdm
{
	
	/// <summary> Node Filter removing duplicates.</summary>
	public class DistinctNodeSequence:NodeSequenceBase
	{
		internal XQValue source;
		internal BasicNode node;
		internal HashSet<BasicNode> seenSet = new HashSet<BasicNode>();
		
		public DistinctNodeSequence(XQValue source)
		{
			this.source = source;
		}
		
		public override Node GetNode()
		{
			return node;
		}
		
		public override BasicNode GetBasicNode()
		{
			return node;
		}
		
		public override bool Next()
		{
			for (; source.Next(); )
			{
				node = source.GetBasicNode();
				// equals and HashCode on Node are based on identity.
				if (!seenSet.Contains(node))
				{
					seenSet.Add(node);
					return true;
				}
			}
			return false;
		}
		
		public override double GetFulltextScore(Item item)
		{
			if (item == null)
				item = GetNode();
			return source.GetFulltextScore(item);
		}
		
		public override XQValue BornAgain()
		{
			return new DistinctNodeSequence(source.BornAgain());
		}
	}
}